package nthu.web.model.business.persistence;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nthu.web.model.domain.Coordinate;
import nthu.web.model.domain.User;


@SuppressWarnings("serial")
public class CurrentUserDao extends ResourceDao<User> {

	@Override
	public void doPost (HttpServletRequest request, HttpServletResponse response)
														throws ServletException, IOException {
	
		ArrayList<User> currentlist = new ArrayList<User>();
	
		currentlist.add(new User("sihalon (資電館)",new Coordinate(24.7948385,120.9922449)));
		currentlist.add(new User("bearhola (總圖)",new Coordinate(24.7945393,120.9934832)));
		currentlist.add(new User("babybobo (宿舍區)",new Coordinate(24.7910632,120.9943482)));
		currentlist.add(new User("marthin (小吃部)",new Coordinate(24.793109,120.992684)));
		
		try{
			File outfile = new File("currentuser.bin");
			ObjectOutputStream objout = new ObjectOutputStream( new FileOutputStream(outfile)); 
			
			// write all Users to the file
			objout.writeObject(currentlist); 
			/*
			for(int i = 0; i < currentlist.size(); i++) 
				objout.writeObject(currentlist.get(i)); 
			*/
			objout.close();
			
			
		}catch(IOException e){
			System.out.println(e.getMessage());
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
													throws ServletException, IOException {
		 ArrayList<User> currentlist;
		try {
			File infile = new File("currentuser.bin");
	        ObjectInputStream objin = new ObjectInputStream(new FileInputStream(infile));

			currentlist = (ArrayList<User>) objin.readObject();
			// we get all we need (the whole list)
	        objin.close();

			setModel(request,currentlist);


	      }

	      catch (IOException e) {
				System.out.println(e.getMessage());
	      }catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		  }
		
	}
	
	
	
	
}
